<template>
    <div>
        <div id="article-preview-modal" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
            <div class="relative w-full max-w-4xl max-h-full">
                <!-- Modal content -->
                <div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
                    <!-- Modal header -->
                    <div class="flex items-start justify-between p-4 border-b rounded-t dark:border-gray-600">
                        <h3 class="text-xl font-semibold text-gray-900 dark:text-white">
                            {{ article.title }}
                        </h3>
                        <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="article-preview-modal">
                            <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
                                <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
                            </svg>
                            <span class="sr-only">关闭</span>
                        </button>
                    </div>
                    <!-- Modal body -->
                    <div class="p-6 space-y-6">
                        <!-- 文章元信息 -->
                        <div class="flex items-center justify-between text-sm text-gray-500 mb-4">
                            <div class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                </svg>
                                {{ article.createTime }}
                            </div>
                            <div v-if="article.categoryName" class="flex items-center">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
                                </svg>
                                {{ article.categoryName }}
                            </div>
                            <div v-if="article.readNum" class="flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
                                </svg>
                                {{ article.readNum }}
                            </div>
                        </div>
                        
                        <!-- 文章封面 -->
                        <div v-if="article.cover" class="mb-4">
                            <img :src="article.cover" @error="handleImageError" class="w-full h-48 object-cover rounded-lg" alt="文章封面">
                        </div>
                        
                        <!-- 文章标签 -->
                        <div v-if="article.tags && article.tags.length > 0" class="flex flex-wrap gap-2 mb-4">
                            <span v-for="tag in article.tags" :key="tag.id" class="bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded cursor-pointer hover:bg-gray-200">
                                {{ tag.name }}
                            </span>
                        </div>
                        
                        <!-- 文章描述 -->
                        <p class="text-base leading-relaxed text-gray-600 dark:text-gray-400">
                            {{ article.description || '暂无描述' }}
                        </p>
                        
                        <!-- 文章内容预览 -->
                        <div v-if="article.content" class="text-base leading-relaxed text-gray-500 dark:text-gray-400 max-h-60 overflow-y-auto preview-content">
                            <div v-html="previewContent"></div>
                        </div>
                    </div>
                    <!-- Modal footer -->
                    <div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
                        <button @click="goArticleDetail" data-modal-hide="article-preview-modal" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">
                            阅读全文
                        </button>
                        <button data-modal-hide="article-preview-modal" type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">
                            关闭
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { initModals } from 'flowbite';

// 初始化flowbite的Modal
onMounted(() => {
    initModals();
});

const router = useRouter();

// 接收父组件传递的文章数据
const props = defineProps({
    article: {
        type: Object,
        default: () => ({})
    }
});

// 计算属性：只显示文章内容的前200个字符
const previewContent = computed(() => {
    if (!props.article.content) return '';
    
    // 清除HTML标签，只保留纯文本
    const tempDiv = document.createElement('div');
    tempDiv.innerHTML = props.article.content;
    const textContent = tempDiv.textContent || tempDiv.innerText || '';
    
    // 截取前200个字符
    return textContent.substring(0, 200) + (textContent.length > 200 ? '...' : '');
});

// 图片加载错误处理
const handleImageError = (event) => {
    event.target.src = 'https://via.placeholder.com/800x400?text=暂无封面';
    console.log('图片加载失败，使用默认图片');
};

// 跳转到文章详情页
const goArticleDetail = () => {
    if (props.article.id) {
        router.push({ 
            path: '/article/detail', 
            query: { articleId: props.article.id } 
        });
    }
};
</script>

<style scoped>
.preview-content {
    border-top: 1px solid #f0f0f0;
    padding-top: 1rem;
    margin-top: 1rem;
}
</style> 